import http from '@/utils/request'
import { removeTokenInfo, setTokenInfo } from '@/utils/storage'

/**
 * 发送短信验证码
 * @param {string} mobile 手机号码
 * @returns thunk
 */
export const sendValidationCode = (mobile) => {
  return async (dispatch) => {
    const res = await http.get(`/sms/codes/${mobile}`)
    console.log(res)
  }
}

export const setToken = (payload) => {
  return {
    type: 'login/token',
    payload,
  }
}

/**
 * 登录
 * @param {{ mobile, code }} values 登录信息
 * @returns thunk
 */
export const login = (data) => {
  return async (dispatch) => {
    const res = await http.post('/authorizations', data)
    const tokenInfo = res.data
    // console.log(tokenInfo)
    dispatch(setToken(tokenInfo))
    setTokenInfo(tokenInfo)
  }
}

/**
 * 退出
 * @returns
 */
export const logout = () => {
  return (dispatch) => {
    removeTokenInfo()
    dispatch({
      type: 'login/logout',
    })
  }
}
